package newcoder_exp.justPractise;

import org.junit.Test;

public class NC100 {
    public class Solution {

        public int atoi (String str) {
            if (str == null || str.length() == 0) return 0;
            str = str.trim();
            if (str.contains(" ")) return 0;
            long res = 0;
            int flag = 1;
            // str = str.replace(" ", "");
            if (str.charAt(0) == '+') {
                flag = 1;
                str = str.substring(1);
            } else if (str.charAt(0) == '-'){
                flag = -1;
                str = str.substring(1);
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < str.length(); i++) {
                char c= str.charAt(i);
                if (c >= '0' && c <= '9') {
                    sb.append(c);
                } else break;
            }
            str = sb.toString();
            if (!str.contains("e") && !str.contains("E")) {
                int d = str.length(), idx = 0;
                
                while (d > 0) {
                    res += (str.charAt(idx) - '0') * (int)Math.pow(10, d - 1);
                    idx++;
                    d--;
                }

            } else {
            }
            if (res > Integer.MAX_VALUE) {
                if (flag == 1) {
                    return Integer.MAX_VALUE;
                } else {
                    return Integer.MIN_VALUE;
                }
            }
            return flag * (int)res;
        }
    }

    @Test
    public void test() {
        Solution s = new Solution();
        int[] nums = new int[]{};
        String str = "    11333713950";
        System.out.println(s.atoi(str));
    }
}
